﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Super.Common
{
    /// <summary> 枚举类型的值
    /// </summary>
    public class EnumValueAttribute : Attribute
    {
        public EnumValueAttribute(params object[] values)
        {
            Values = values;
        }
        public object[] Values { get; private set; }
    }

    /// <summary> 枚举辅助对象
    /// </summary>
    public static partial class EnumHelper
    {
        /// <summary> 所有枚举类型包含的值.
        /// </summary>
        public static Dictionary<Type, Dictionary<int, object[]>> EnumValue = new Dictionary<Type, Dictionary<int, object[]>>();

        /// <summary> 注册枚举类型
        /// </summary>
        public static void RegEnumType(Type enumType)
        {
            var evs = Enum.GetValues(enumType);

            var di = evs.Cast<object>()
                .ToDictionary
                (
                     ev => (int)ev,
                     ev => (ev.GetType().GetField(ev.ToString())
                                .GetCustomAttributes(true).FirstOrDefault(j => j is EnumValueAttribute) as EnumValueAttribute).Values
               
                );
            EnumValue.Add(enumType, di);
        }

        /// <summary> 获取枚举的扩展值.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <param name="valueInx"></param>
        /// <returns></returns>
        public static T GetEnumValue<T>(object enumValue, int valueInx)
        {
#if DEBUG
            if (!EnumValue.ContainsKey(enumValue.GetType()))
            {
                throw new Exception("使用了未注册的枚举类型! " + EnumValue.GetType().Name);
            }
            var et = EnumValue[enumValue.GetType()];

            var etv = et[(int)enumValue];
            if (etv == null)
            {
                throw new Exception("枚举类型" + enumValue.GetType().Name + " 中没有 " + enumValue + "字段!");
            }

            if (etv.Length <= valueInx)
            {
                throw new Exception(enumValue.GetType().Name + "." + enumValue + " 不包含第{0}个索引".FormatMe(valueInx));
            }

            return (T)etv[valueInx];
#endif
            return (T)EnumValue[enumValue.GetType()][(int)enumValue][valueInx];
        }

        /// <summary> 根据枚举的扩展值获取枚举
        /// </summary>  
        /// <param name="enumValue"> </param>
        /// <param name="valueInx"></param>
        /// <returns></returns>
        public static T FromEnumValue<T>(object enumValue, int valueInx)
        {
            var ev = EnumValue[typeof(T)].FirstOrDefault(j => j.Value[valueInx].Equals(enumValue));
            return FormInteger<T>(ev.Key);
        }

        /// <summary> 自定义查询.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IEnumerable<KeyValuePair<Type, Dictionary<int, object[]>>> Query(Type type)
        {
            return EnumValue.Where(j => j.Key == type);
        }

        /// <summary> Convert String To Enum Object 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Parse<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value);
        }

        /// <summary> Convert int to Enum Object 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T FormInteger<T>(int value)
        {
            return (T)(object)value;
        }
    }


}
